import {
  Body,
  Controller,
  Inject,
  Logger,
  LoggerService,
  Post,
} from "@nestjs/common";
import { PanelService } from "./panel.service";
import { GlobalStatusService } from "../global/global-status/global-status.service";
import { OnEvent } from "@nestjs/event-emitter";
import { TransportWithinServiceService } from "../micro/transport/transport-within-service.service";
import { Constants } from "../../../../types/types";

@Controller("panel")
export class PanelController {
  private readonly logger = new Logger(PanelController.name);

  constructor(
    private readonly panelService: PanelService,
    private readonly globalStatusService: GlobalStatusService,
    private readonly withGateway: TransportWithinServiceService,
  ) {}

  @Post("send-schedule")
  async handleScan(@Body() scanParams: any): Promise<any> {
    console.log("收到scan请求", scanParams);
    return await this.panelService.handleScan(scanParams);
  }

  @Post("switchModeToScan")
  async handleEnterScan(@Body() scanParams: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      data: "",
      msg: "success",
    };
  }

  @Post("switchModeToService")
  async handleEnterService(@Body() scanParams: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      data: "",
      msg: "success",
    };
  }

  @Post("switchModeToDefault")
  async handleEnterHome(@Body() scanParams: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      data: "",
      msg: "sdfsdfsdfs",
    };
  }

  @Post("setLaser")
  async handleSetLaser(@Body() params: any): Promise<any> {
    console.log(new Date().toISOString(), `setLaser`);

    this.logger.log("set laser");
    this.globalStatusService.toggleLaser();
    if (params.laserFlag) {
      this.withGateway.sendSysMsg({
        type: "laser",
        data: true,
        msg: "success",
      });

      setTimeout(() => {
        this.withGateway.sendSysMsg({
          type: "laserCurrentPosition",
          data: {
            current: 200,
          },
          msg: "success",
        });
      }, 2000);
      setTimeout(() => {
        this.withGateway.sendSysMsg({
          type: "laserCurrentPosition",
          data: {
            current: 300,
          },
          msg: "success",
        });
      }, 4000);
    } else {
      this.withGateway.sendSysMsg({
        type: "laser",
        data: false,
        msg: "success",
      });
    }
    return {
      code: Constants.SuccessCode,
      data: "",
      msg: "success",
    };
  }

  @Post("getDiskUseList")
  async handleGetDiskUseList(@Body() params: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      data: [
        {
          id: 1,
          diskName: "c",
          usedInfo: {
            used: 180,
            total: 200,
          },
        },
        {
          id: 2,
          diskName: "d",
          usedInfo: {
            used: 180,
            total: 200,
          },
        },
      ],
      msg: "sdfsdfsdfs",
    };
  }

  @Post("getDiskAlertList")
  async handleGetDiskAlertList(@Body() params: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      data: [
        {
          id: 1,
          upperLimit: "50",
        },
        {
          id: 2,
          upperLimit: "50",
        },
      ],
      msg: "success",
    };
  }

  @Post("systemError")
  async handleGetSystemError(@Body() params: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      data: [
        {
          id: 0,
          name: "123",
          level: "warning",
          description: "zczxczxcz",
          cause: "caused by",
          status: 0,
        },
        {
          id: 1,
          name: "123",
          level: "warning",
          description: "zczxczxcz",
          cause: "caused by",
          status: 1,
        },
        {
          id: 2,
          name: "123",
          level: "warning",
          description: "zczxczxcz",
          cause: "caused by",
          status: 2,
        },
        {
          id: 3,
          name: "123",
          level: "warning",
          description: "zczxczxcz",
          cause: "caused by",
          status: 3,
        },
      ],
      msg: "",
    };
  }

  @Post("ballTubePreheating")
  async handleBallTubePreheating(@Body() params: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      msg: "success",
      data: "",
    };
  }

  @Post("patientCTTree")
  async handlePatientCTTree(@Body() params: any): Promise<any> {
    return {
      code: Constants.SuccessCode,
      msg: "success",
      data: [
        {
          id: 1,
          level: 1,
          patientName: "zhangs3",
          patientId: "zhangs3sdffds",
          children: [
            {
              id: 11,
              level: 2,
              labelValue: "文件夹",
              children: [
                {
                  id: 111,
                  level: 3,
                  labelValue:
                    "扫描1111dfgsfdggggggggggggggggggggggggggggggggggghgreghreyrthfghfg",
                  type: "helical",
                  imgAddress: [
                    {
                      studyUID: "dsd",
                      seriesUID: "sdssds",
                    },
                  ],
                },
                {
                  id: 112,
                  level: 3,
                  labelValue: "扫描11ds11",
                  type: "scout",
                  desc: "双平片",
                  imgAddress: [
                    {
                      studyUID: "dsd",
                      seriesUID: "sdssds",
                    },
                    {
                      studyUID: "dsd",
                      seriesUID: "sdssds",
                    },
                  ],
                },
                {
                  id: 113,
                  level: 3,
                  labelValue:
                    "扫描1111dfgsfdggggggggggggggggggggggggggggggggggghgreghreyrthfghfg",
                  type: "fault",
                  imgAddress: [
                    {
                      studyUID: "dsd",
                      seriesUID: "sdssds",
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    };
  }

  @Post("deleteImgById")
  async handleDeleteImgById(@Body() params: any): Promise<any> {
    return {
      code: 200,
      msg: "success",
      data: "",
    };
  }

  @OnEvent("global.status.change")
  test() {
    console.log("event triggerr");
  }
}
